//
//  ListenViewController.m
//  SuperNews
//
//  Created by dllo on 16/1/14.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "ListenViewController.h"
#import "ListenTableViewCell.h"
#import "ListenTypeModel.h"
#import "ListenMoreListModel.h"
#import "RadioModel.h"
#import "HeaderTableViewCell.h"
#import "ListenListMoreViewController.h"
#import "PlayerViewController.h"

#define MAINURL @"http://c.3g.163.com/nc/topicset/ios/radio/index.html"
#define MOREURL @" http://c.3g.163.com/nc/topicset/ios/radio/C1420525588210/0-20.html"
#define MUSICURL @"http://c.3g.163.com/nc/article/BCO5IN7D00964KM4/full.html"
#define LISTURL @"http://c.3g.163.com/nc/article/list/T1394711522757/0-20.html"

@interface ListenViewController () <UITableViewDelegate, UITableViewDataSource, ListenTableViewCellDelegate>

@property (nonatomic, strong) UITableView *tableV;
@property (nonatomic, strong) NSMutableArray *cListArr;
@property (nonatomic, strong) NSMutableArray *tListArr;

@end

@implementation ListenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.cListArr = [NSMutableArray array];
    self.tListArr = [NSMutableArray array];
    [self createViews];
    
    [self getData];
    
}

- (void)createViews
{
    self.view.backgroundColor = [UIColor whiteColor];
    [self createTableView];
}

- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 104) style:UITableViewStyleGrouped];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    
    [self.tableV registerClass:[ListenTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableV registerClass:[HeaderTableViewCell class] forCellReuseIdentifier:@"headerCell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.cListArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.row) {
        return 50;
    } else {
        return 200;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.row) {
        ListenMoreListModel *listenModel = [self.cListArr objectAtIndex:indexPath.section];
        HeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"headerCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSString *str = [NSString stringWithFormat:@"%@.png", listenModel.cname];
        cell.imageV.image = [UIImage imageNamed:str];
        cell.label.text = listenModel.cname;
        return cell;
    } else {
        NSMutableArray *typeArr = [self.tListArr objectAtIndex:indexPath.section];
        ListenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        cell.cArr = typeArr;
        return cell;
    }
}

- (void)listenTableViewCellHandle:(RadioModel *)radio listid:(NSString *)listid
{
    PlayerViewController *playerVC = [PlayerViewController sharePlayerViewController];
    playerVC.cid = listid;
    playerVC.docid = radio.docid;
    [self.navigationController pushViewController:playerVC animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListenMoreListModel *listModel = [self.cListArr objectAtIndex:indexPath.section];
    ListenListMoreViewController *listVC = [[ListenListMoreViewController alloc] init];
    listVC.cname = listModel.cname;
    listVC.cid = listModel.cid;
    [self.navigationController pushViewController:listVC animated:YES];
}

- (void)getData
{
    [AFNetTool getNetWithURL:MAINURL body:nil headFile:nil responseStyle:YZQJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        NSMutableArray *cArr = [responseObject objectForKey:@"cList"];
        self.cListArr = [ListenMoreListModel baseModelWithArr:cArr];
        for (NSDictionary *tempDic in cArr) {
            NSMutableArray *tArr = [tempDic objectForKey:@"tList"];
            NSMutableArray *radioArr = [NSMutableArray array];
            for (NSDictionary *tmpDic in tArr) {
                ListenTypeModel *typeModel = [[ListenTypeModel alloc] init];
                NSDictionary *radioDic = [tmpDic objectForKey:@"radio"];
                RadioModel *radioModel = [[RadioModel alloc] init];
                [radioModel setValuesForKeysWithDictionary:radioDic];
                typeModel.radio = radioModel;
                typeModel.tid = [tmpDic objectForKey:@"tid"];
                [radioArr addObject:typeModel];
            }
            [self.tListArr addObject:radioArr];
        }
        [self.tableV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
